//
//  HX_SQLiteManger.m
//  UI17 - Mail-List
//
//  Created by 韩旭 on 16/8/8.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "HX_SQLiteManger.h"
#import "MailModel.h"
NS_ASSUME_NONNULL_BEGIN
@implementation HX_SQLiteManger
+ (HX_SQLiteManger *)shareManger {
    static HX_SQLiteManger *manger = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manger = [[HX_SQLiteManger alloc] init];
    });
    return manger;
}
- (BOOL)openSqlite3 {
    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *document = [path firstObject];
    NSString *pathName = [document stringByAppendingPathComponent:@"sqlite3.db"];
    NSLog(@"%@", pathName);
    int result = sqlite3_open(pathName.UTF8String, &dbPoint);
    return [self isOperationResult:result alertText:@"打开"];
}

- (BOOL)isOperationResult:(int)result alertText:(NSString *)alerText {
    BOOL isOperation = YES;
    if (result == SQLITE_OK) {
        isOperation = YES;
        NSLog(@"数据库%@成功", alerText);
    } else {
        NSLog(@"数据库%@失败", alerText);
        isOperation = NO;
    }
    return isOperation;
}
- (BOOL)creatTable {
    NSString *creatSql = @"create table if not exists hx_Mail (hx_id integer primary key autoincrement, hx_name text not null, hx_Image text)";
    int result = sqlite3_exec(dbPoint, creatSql.UTF8String, nil, nil, nil);
    return [self isOperationResult:result alertText:@"创建"];
}
- (BOOL)insertModel:(MailModel *)mailModel
{
    NSString *insertSQL = [NSString stringWithFormat:@"insert into hx_Mail (hx_id, hx_name ,hx_image) values (null, '%@', '%@')", mailModel.name, mailModel.pathImage];
    int result = sqlite3_exec(dbPoint, insertSQL.UTF8String, nil, nil, nil);
    return [self isOperationResult:result alertText:@"插入"];
}
- (NSArray *)selectName {
    NSString *selectAll = @"select * from hx_Mail";
    sqlite3_stmt *stmt = nil;
    int result = sqlite3_prepare(dbPoint, selectAll.UTF8String, -1, &stmt, nil);
    NSMutableArray *hx_arr = [NSMutableArray array];
    if (result == SQLITE_OK) {
        while (sqlite3_step(stmt) == SQLITE_ROW) {
//            int hx_id = sqlite3_column_int(stmt, 0);
            const unsigned char *hx_name = sqlite3_column_text(stmt, 1);
            const unsigned char *hx_image = sqlite3_column_text(stmt, 2);
            MailModel *model = [[MailModel alloc] init];
            model.name = [NSString stringWithUTF8String:(const char*)hx_name];
            model.pathImage = [NSString stringWithUTF8String:(const char*)hx_image];
            [hx_arr addObject:model];
        }
    }
//    销毁对象
    sqlite3_finalize(stmt);
    return hx_arr;
}

- (BOOL)deleteAll {
    NSString *deleteSQL = @"delete from hx_Mail where 1=1";
    int result = sqlite3_exec(dbPoint, [deleteSQL UTF8String], nil, nil, nil);
    
    return [self isOperationResult:result alertText:@"删除全部"];
}

//- (BOOL)deleteSQLite:(MailModel *)model {
//    NSString *deletSql = [NSString stringWithFormat:@"delete from hx_Mail where stu_id = %ld", stu.stu_id];
//    int result = sqlite3_exec(dbPoint, [deletSql UTF8String], nil, nil, nil);
//    NSString *alertText = [NSString stringWithFormat:@"删除%ld条数据", stu.stu_id];
//    return [self isCloseOperationResult:result alertText:alertText];
//}


@end
NS_ASSUME_NONNULL_END
